package com.ppc.ugo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ppc.ugo.dao.mapper.FloorDataMapper;
import com.ppc.ugo.dao.pojo.Floor;
import com.ppc.ugo.dao.pojo.FloorData;
import com.ppc.ugo.service.FloorDataService;
import com.ppc.ugo.service.FloorService;
import com.ppc.ugo.vo.FloorDataVo;
import com.ppc.ugo.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
@Slf4j
public class FloorDataServiceImpl implements FloorDataService {
    @Resource
    private FloorDataMapper floorDataMapper;

    @Autowired
    private FloorService floorService;

    @Override
    public void addFloorData(FloorData floorData) {
        floorDataMapper.insert(floorData);
        log.info("新增楼层商品数据：{}", floorData.getName());
    }

    @Override
    public Result getFloorData() {
        LambdaQueryWrapper<FloorData> queryWrapper = new LambdaQueryWrapper<>();
        List<FloorDataVo> floorDataVoList = new ArrayList<>();

        List<Floor> floors = floorService.floorList();
        for (Floor floor : floors) {
            Integer floorId = floor.getId();

            queryWrapper.eq(FloorData::getFloor_id, floorId);
            List<FloorData> floorDataList = floorDataMapper.selectList(queryWrapper);

            FloorDataVo floorDataVo = new FloorDataVo();
            floorDataVo.setFloor_title(floor);
            floorDataVo.setProduct_list(floorDataList);

            floorDataVoList.add(floorDataVo);

            queryWrapper.clear();
        }

        return Result.success(floorDataVoList);

    }


}
